package com.infoauto.opc.gateway.opc.model;

import com.infoauto.opc.gateway.opc.utils.HttpClientUtil;
import com.infoauto.opc.gateway.opc.utils.HttpVisitException;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

/**
 * 第三方接口实体
 * */
@Getter
@Setter
public class WebInterface {
    private int thirdID;
    private String thirdInterfaceCode;
    private String thirdInterfaceName;
    private String callbackUrl;
    private String description;
    private int receiveDataType;
    private int workDirection;
    private Map<Integer, WebInterfaceNode> nodes;

    /**
     * 第三方接口数据生成json格式
     * **/
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"thirdInterfaceCode\":\"");
        sb.append(this.thirdInterfaceCode);
        sb.append("\",\"thirdInterfaceName\":\"");
        sb.append(this.thirdInterfaceName);
        sb.append("\",\"callbackUrl\":\"");
        sb.append(this.callbackUrl);
        sb.append("\",\"description\":\"");
        sb.append(this.description);
        sb.append("\",\"Values\":{");
        for(WebInterfaceNode in:nodes.values()){
            sb.append(in.toString());
            sb.append(",");
        }
        if (nodes.size()>0)
        {
            sb = sb.deleteCharAt(sb.length()-1);
        }
        sb.append("}}");
        return  sb.toString();
    }

    /**
     * 执行远程回调
     * **/
    public void doCallBack() {
        try {
            HttpClientUtil.doPostRequest(this.callbackUrl,"ANDON_SEND",this.toString());
        } catch (HttpVisitException e) {

        }
    }
}
